package com.taptech.doufu.services;

import android.app.Activity;

import com.taptech.doufu.util.StringUtil;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.net.httputils.HttpRequestObject;
import com.taptech.doufu.net.httputils.HttpUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;



public class DiaobaoSearchService {
	public static final int HANDLE_TYPE_SEARCH_USER = 4002;
	public static final int HANDLE_TYPE_SEARCH_ARTICLE = 4003;
	public static final int HANDLE_TYPE_SEARCH_CARTOON = 4006;
	public static final int HANDLE_TYPE_SEARCH_TAG = 4007;
	private static DiaobaoSearchService instance = new DiaobaoSearchService();

	private DiaobaoSearchService() {

	}

	public static DiaobaoSearchService getInstance() {
		return instance;
	}


	//	2,3,5,6,9,28,29
	public void searchArticle(String articleName, int pageIndex,
							  HttpResponseListener listener,
							  String types,
							  String order,
							  String is_finished,
							  String vip,int singlePage) {
		if(!StringUtil.stringLength(types)){
			types = "16,18,29,41,42,43";
		}
		HttpRequestObject request = new HttpRequestObject();
		request.setRequestData(articleName);
		request.setHandleType(HANDLE_TYPE_SEARCH_ARTICLE);
		try {
			StringBuilder url = new StringBuilder(Constant.BASE_URL);
			url.append("search/content?keyword=").append(URLEncoder.encode(articleName,"UTF-8"));
			url.append("&start=").append(pageIndex * Constant.PAGE_SIZE_20);
			url.append("&rows=").append(Constant.PAGE_SIZE_20);
			url.append("&types=").append(types);
			url.append("&order=").append(order);
			url.append("&is_finished=").append(is_finished);
			url.append("&vip=").append(vip);
			url.append("&single_page=").append(singlePage);
			request.setUrl(url.toString());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		request.setListener(listener);
		request.setPageIndex(pageIndex);
//		if (pageIndex == 0) {
//			HttpUtil.sendLockedGetRequest(request,activity);
//		} else {
		HttpUtil.sendGetRequest(request);
//		}

	}

	/**
	 * 搜索标签
	 */
	public void searchHomeTag( HttpResponseListener listener,String tagName,String last) {
		HttpRequestObject request = new HttpRequestObject();
		request.setRequestData(tagName);
		request.setHandleType(HANDLE_TYPE_SEARCH_TAG);
		try {
			request.setUrl(Constant.HOST_NAME_SECOND + "/search/tag_info?search&keyword="
					+  URLEncoder.encode(tagName,"UTF-8") + "&rows=" + Constant.PAGE_SIZE_20 +"last=" +last);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		request.setListener(listener);
		request.setPageIndex(0);

		HttpUtil.sendGetRequest(request);

	}

	public void searchHomeUser(String userName, int pageIndex,
							   HttpResponseListener listener,Activity activity) {
		HttpRequestObject request = new HttpRequestObject();
		request.setRequestData(userName);
		request.setHandleType(HANDLE_TYPE_SEARCH_USER);
		try
		{
			request.setUrl(Constant.HOST_NAME_SECOND + "/index.php/search/user?nickname=" + URLEncoder.encode(userName, "UTF-8") + "&size=" + Constant.PAGE_SIZE_20 + "&begin=" + pageIndex * Constant.PAGE_SIZE_20);
		}catch (Exception e){
			e.printStackTrace();
		}
		request.setListener(listener);
		request.setPageIndex(pageIndex);
		HttpUtil.sendGetRequest(request);

	}

	/**
	 * 搜索漫画
	 * @param cartoonName
	 * @param pageIndex
	 * @param listener
	 * @param activity
	 */
	public void searchHomeCartoon(String cartoonName, int pageIndex, HttpResponseListener listener,Activity activity ,String last) {
		HttpRequestObject request = new HttpRequestObject();
		request.setRequestData(cartoonName);
		request.setHandleType(HANDLE_TYPE_SEARCH_CARTOON);
		try
		{
			request.setUrl(Constant.HOST_NAME_SECOND + "/search/comics?search&keyword=" + URLEncoder.encode(cartoonName,"UTF-8") + "&size=" + Constant.PAGE_SIZE_20 +
					"&begin=" + pageIndex * Constant.PAGE_SIZE_20 + "&last=" +last);
		}catch (Exception e){
			e.printStackTrace();
		}
		request.setListener(listener);
		request.setPageIndex(pageIndex);
		if (pageIndex == 0) {
			HttpUtil.sendLockedGetRequest(request,activity);
		} else {
			HttpUtil.sendGetRequest(request);
		}

	}
}
